package project.bluetooth.com.loadsirproject.network.bean;

import android.os.Parcel;
import android.os.Parcelable;

import com.google.gson.Gson;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

/**
 * Created by lgs on 2018/1/14.
 * 返回的是集合json通用类
 */

public class BListResponse<T> implements Parcelable {


    /**
     * code : 0
     * msg : 验证码发送成功
     * data : {}
     */

    private String code;
    private String msg;

    /**
     * 数据
     */
    private List<T> data;

    public BListResponse() {
    }

    protected BListResponse(Parcel in) {
        code = in.readString();
        msg = in.readString();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(code);
        dest.writeString(msg);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<BListResponse> CREATOR = new Creator<BListResponse>() {
        @Override
        public BListResponse createFromParcel(Parcel in) {
            return new BListResponse(in);
        }

        @Override
        public BListResponse[] newArray(int size) {
            return new BListResponse[size];
        }
    };

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public static BListResponse fromJson(String json, Class clazz) {
        Gson gson = new Gson();
        Type objectType = type(BListResponse.class, clazz);
        return gson.fromJson(json, objectType);
    }

    public String toJson(Class<T> clazz) {
        Gson gson = new Gson();
        Type objectType = type(BListResponse.class, clazz);
        return gson.toJson(this, objectType);
    }

    private static ParameterizedType type(final Class raw, final Type... args) {
        return new ParameterizedType() {
            public Type getRawType() {
                return raw;
            }

            public Type[] getActualTypeArguments() {
                return args;
            }

            public Type getOwnerType() {
                return null;
            }
        };
    }
}
